<template>
  <div class="login">
    <particles></particles>
    <div class="login-wrapper">
      <div class="login-header">
        <h2>宿舍登录</h2>
      </div>
      <div class="login-container">
          <div class="form-box">
            <el-form class="loginForm" :model="addModel" :rules="rules" ref="loginForm">
              <el-form-item prop="username">
              <div class="input-box">
                <i class="iconfont icon-zhanghao"></i>

                <input type="text" v-model="addModel.username">

                <label :class="addModel.username.length !== 0 ? 'label-up' : ''">账号</label>
              </div>
              </el-form-item>
            <el-form-item prop="password">
              <div class="input-box">
                <i class="iconfont icon-mima"></i>
                <input  type="password" v-model="addModel.password">
                <label :class="addModel.password.length !== 0 ? 'label-up' : ''">密码</label>
              </div>
            </el-form-item>
            <el-form-item prop="userType">
              <el-radio-group v-model="addModel.userType">
                <el-radio :label="0">学生</el-radio>
                <el-radio :label="1">宿管</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item>
              <el-button class="login-btn" plain @click="onSubmit">登录</el-button>
            </el-form-item>
            </el-form>
          </div>
      </div>
    </div>
  </div>

</template>

<script>
import Particles from '../../assets/particles/index.vue'

export default {
  components: {Particles},
  data() {
    return {
      addModel: {
        username: '',
        password: '',
        userType: '',
      },
      rules: {
        username: [{trigger: 'blur', required: true, message: '请输入账号'}],
        password: [{trigger: 'blur', required: true, message: '请输入密码'}],
        userType: [{trigger: 'blur', required: true, message: '请选择类型'}]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          console.log('表单验证成功！');
          this.$store.dispatch('user/login', this.addModel).then(() => {
            this.$router.push({ path: this.redirect || '/' })
          })
        } else {
          // 表单验证失败，显示错误信息
          console.log('表单验证失败！');
          return false;
        }
      });
    }
  }
}
</script>
<style scoped>
.login-wrapper {
  position: absolute;
  width: 400px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  backdrop-filter: blur(15px);
  box-shadow: 0px 0px 30px rgba(0, 0, 0, 0.5);
}

.login-header {
  position: relative;
}

.login-header h2 {
  position: absolute;
  top: -70px;
  left: 50%;
  font-weight: 600;
  transform: translateX(-50%);
  font-size: 3em;
  color: #fff;
  text-shadow: 0 10px 15px rgb(0, 0, 0);
}

.login-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
}

.form-box {
  width: 100%;
  padding: 10px 40px 40px 40px;
}

.input-box {
  position: relative;
  width: 100%;
  height: 50px;
  border-bottom: 2px solid #162938;
  margin: 30px 0;
}

.input-box label {
  position: absolute;
  top: 50%;
  left: 5px;
  transform: translateY(-50%);
  font-size: 1em;
  color: #162938;
  pointer-events: none;
  transition: .5s;
}

.input-box input:focus~label, .label-up {
  top: -5px !important;
}

.input-box input {
  width: 100%;
  height: 100%;
  background: transparent;
  border: none;
  outline: none;
  font-size: 1em;
  color: #162938;
  font-weight: 600;
  padding: 0 35px 0 5px;
}

.input-box .iconfont {
  position: absolute;
  right: 0px;
  padding-right: 8px;
  font-size: 1.2em;
  color: #162938;
  line-height: 48px;
}

.login-btn {
  width: 100%;
  height: 45px;
  border-radius: 6px;
  font-size: 1.1em;
}

.tips {
  font-size: .9em;
  color: rgb(31, 61, 84);
  margin-top: 20px;
}

.tips a {
  cursor: pointer;
  color: var(--Pi6_u);
  text-decoration: none;
}

.tips a:hover {
  text-decoration: underline;
}

@media (max-width: 444px) {
  .form-box {
    padding: 10px 40px 20px 40px;
  }

  .login-wrapper {
    width: 90vw;
  }

  .input-box {
    margin: 18px 0;
    height: 40px;
  }

  .login-btn {
    height: 40px;
  }
}
</style>
<style>
.el-form-item {
   margin-bottom: 0;
}
</style>


